import { requestClient } from '#/api/request';

export interface PageFetchParams {
  page: number;
  pageSize: number;
  search?: string;
}

// 添加字段配置接口
export interface FieldConfig {
  field: string;
  title: string;
  component: string;
  required?: boolean;
  width?: number;
  helper?: string;
  default?: string;
  componentProps?: {
    optionConfig?: {
      type: 'enum' | 'service';
      source: string;
      dependOn?: string;
    };
    placeholder?: string;
    style?: string;
    options?: Array<{ label: string; value: any }>;
  };
  cascades?: string[];
  inTable?: boolean;    // 表格列属性
  enable?: boolean;     // 只读属性
  visible?: boolean;    // 隐藏属性
  rule?: string[];      // 校验规则
  ruleLength?: {        // 长度校验规则
    min?: number;
    max?: number;
  };
  storage?: boolean;  // 添加存储标志字段
  sort?: number;      // 添加排序字段
  [key: string]: any;
}

// 添加选项类型定义
export interface Options {
  label: string;
  value: string;
}

/**
 * 保存数据
 * @param tableName 表名
 * @param data 数据
 */
export function saveLowcodeApi(tableName: string, data: Record<string, any>) {
  return requestClient.post<Record<string, any>>(`/lowcode/${tableName}/save`, data);
}

/**
 * 删除数据
 * @param tableName 表名
 * @param id ID
 */
export function deleteLowcodeApi(tableName: string, id: string) {
  return requestClient.delete(`/lowcode/${tableName}/delete?id=${id}`);
}

/**
 * 获取列表数据
 * @param tableName 表名
 * @param params 查询参数
 */
export function getLowcodeListApi(tableName: string, params: PageFetchParams) {
  return requestClient.get<Record<string, any>>(`/lowcode/${tableName}/list`, {
    params: {
      page: params.page,
      pageSize: params.pageSize,
      search: params.search,
    },
  });
}

/**
 * 获取详情
 * @param tableName 表名
 * @param id ID
 */
export function getLowcodeDetailApi(tableName: string, id: string) {
  return requestClient.get<Record<string, any>>(`/lowcode/${tableName}/${id}`);
}

/**
 * 获取Schema数据
 * @param redirect 重定向地址
 */
export function getSchemaApi(redirect: string) {
  return requestClient.get<Record<string, any>>('/lowcode/designer/schema', {
    params: { redirect },
  });
}

// 添加获取选项列表的API
export function getAllOptionListInTable(tableName: string) {
  return requestClient.get<Options[]>('/global/getAllOptionListInTable', {
    params: { tableName },
  });
}
